import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-02
 * Time:10:41
 */
//栈是使用顺序表实现的
public class MyStack {
        public int[] elem;
        public int usedSize;//当前栈的长度和栈顶元素的上一个位置
        public MyStack() {
                this.elem = new int[5];
        }
        //压栈
        public void push(int val) {
                if(isFull()){
                        System.out.println("栈满");
                        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
                        System.out.println("扩容成功");
                        return;
                }
                this.elem[this.usedSize++] = val;
                System.out.println(val + "添加成功");
        }

        public boolean isFull() {
                return this.usedSize == this.elem.length;
        }
        //弹出栈顶元素
        public int pop() {
                if(empty()){
                        throw new RuntimeException("栈空");
                }
                int ret = this.elem[usedSize - 1];
                this.usedSize--;
                return ret;
        }

        public boolean empty() {
                return this.usedSize == 0;
        }
        //只是获取栈顶元素 但是不删除
        public int peek() {
                if(empty()){
                        throw new RuntimeException("栈空");
                }
                return this.elem[usedSize - 1];
        }
}